<?php
class user_listController extends adminController{

    //首页
    public function index() {
        //获取管理员列表
        $current_user=$this->getLogin();
        $this->list=model('admin_user')->user_list($current_user);
        $this->show();  
    }

    //添加
    public function add() {
        $this->action_name='添加';
        $this->action='add';
        $this->user_group=model('admin_group')->group_list();
        $this->show('user_list/info'); 
    }

    public function add_save() {
        //录入模型处理
        if($_POST['password']<>$_POST['password2']){
            $this->msg('两次密码输入不同！',0);
            return;
        }
        if(model('admin_user')->user_count($_POST['username'])){
            $this->msg('帐号不能重复！',0);
            return;
        }
        $_POST['password']=md5($_POST['password']);
        model('admin_user')->add($_POST);
        $this->msg('管理员添加成功！',1);
    }

    //修改
    public function edit() {
        $uid=$_GET['uid'];
        $this->alert_str($uid,'int');
        $this->info=model('admin_user')->info($uid);
        $this->action_name='编辑';
        $this->action='edit';
        $this->user_group=model('admin_group')->group_list();
        $this->info_group=model('admin_group')->info($this->info['gid']);
        $current_user=$this->getLogin();
        if($this->info_group['level']<$current_user['level']){
            $this->msg('请勿越权操作！',0);
        }
        $this->show('user_list/info'); 
    }

    public function edit_save() {
        $uid=$_POST['uid'];
        $this->alert_str($uid,'int',true);
        //录入模型处理
        if (!empty($_POST['password']))
        {
            if (empty($_POST['password2']))
            {
               $this->msg('未填写确认密码！',0);
               return;
            }
            if($_POST['password']<>$_POST['password2']){
                $this->msg('两次密码输入不同！',0);
                return;
            }
            if(model('admin_user')->user_count($_POST['username'],$uid)){
                $this->msg('帐号不能重复！',0);
                return;
            }
            $_POST['password']=md5($_POST['password']);
        }else{
            unset($_POST['password']);
        }
        model('admin_user')->edit($_POST);
        $this->msg('管理员修改成功! ',1);
    }

    //删除
    public function del() {
        $uid=intval($_POST['uid']);
        $this->alert_str($uid,'int',true); 
        //录入模型处理
        $info=model('admin_user')->info($uid);
        if($info['keep']==1){
            $this->msg('内置管理员无法删除！',0);
        }
        $info_group=model('admin_group')->info($info['gid']);
        $current_user=$this->getLogin();
        if($info_group['level']<$current_user['level']){
            $this->msg('请勿越权操作！',0);
        }
        model('admin_user')->del($uid);
        $this->msg('管理员删除成功！',1);
    }


}